import store from './vuex/store.js'
export const goTo = async function(params){
	if(params.path === '')return
	if(params.login){
		let user = uni.getStorageSync("userInfo") || {}
		let token = user.token || ""
		if(token === '' || token === null){
			params.url="/pages/login/index"
		}
		return
	}
	// console.log(params.url)
	await uni.navigateTo({
		url:params.url,
		fail(e) {
			console.log(e)
			// uni.showToast({
			// 	title:"页面路径错误",
			// 	icon:'none'  
			// })  
		}
	}) 
} 
export const reditTo = async function(params){
	if(params.path === '')return
	if(params.login){
		let user = uni.getStorageSync("userInfo") || {}
		let token = user.token || ""
		if(token === '' || token === null){
			params.url="/pages/login/index"
		}
		return
	}
	await uni.redirectTo({
		url:params.url,
		fail(e) {
			// uni.showToast({
			// 	title:"路径错误",
			// 	icon:'none'  
			// })  
		}
	}) 
} 
export const goBack = function(delta = 1){
	uni.navigateBack({
		delta:delta,
		fail() {
			goSwitch({url:'/pages/index/index',current:1})
		}
	})
}
export const goreLanch = function(url){
	uni.reLaunch({
		url:url,
		fail() {
			goSwitch({url:'/pages/index/index',current:1})
		}
	})
}
export const goSwitch = async function(params){
	if(params.url === '')return
	if(params.login){
		if(isLogin()){
			uni.navigateTo({
				url:'/pages/login/index',
				// fail(e) {
				// 	uni.showToast({
				// 		title:"路径错误",
				// 		icon:'none'
				// 	})
				// }
			})
		}
	}
	await uni.switchTab({
		url:params.url,
		success() {
			store.commit("changetabbar",params.current)
		},
		fail(e) {
			// uni.showToast({
			// 	title:"路径错误",
			// 	icon:'none'
			// })
		}
	})
}
const isLogin = function(){
	let token = uni.getStorageSync("token") || null,flag = false
	if(token === '' || token === null || !!!token){
		flag = true
	}
	return flag
} 
export default {
	goTo,
	goSwitch,
	goBack,
	goreLanch,
	reditTo
}